package org.crystalrange.documents.web;

import java.util.Set;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.documents.model.Document;
import org.crystalrange.documents.model.DocumentGroup;
import org.crystalrange.documents.service.DocumentGroupService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

public class DocumentGroupController extends BaseController {

	private DocumentGroup documentGroup;
	private DocumentGroupService documentGroupService;
	protected Listbox listboxDocument;
	protected Listbox listboxParentDocumentGroup;
	protected Listbox listboxChildDocumentGroup;

	public DocumentGroup getDocumentGroup() {
		return documentGroup;
	}

	public void setDocumentGroup(DocumentGroup documentGroup) {
		this.documentGroup = documentGroup;
	}

	public DocumentGroupService getDocumentGroupService() {
		return documentGroupService;
	}

	public void setDocumentGroupService(
			DocumentGroupService documentGroupService) {
		this.documentGroupService = documentGroupService;
	}

	public void onClick$toolbarbuttonDocumentGroupSave(Event event)
			throws Exception {
		this.documentGroupSave();
	}

	public void onClick$menuitemDocumentGroupSave(Event event) throws Exception {
		this.documentGroupSave();
	}

	public void onClick$menuitemDocumentAdd(Event event) throws Exception {
		this.documentAdd();
	}

	public void onClick$menuitemDocumentRemove(Event event)
			throws Exception {
		this.documentRemove();
	}
	
	public void onClick$toolbarbuttonDocumentAdd(Event event) throws Exception {
		this.documentAdd();
	}

	public void onClick$toolbarbuttonDocumentRemove(Event event)
			throws Exception {
		this.documentRemove();
	}

	public void onClick$menuitemParentDocumentGroupAdd(Event event)
			throws Exception {
		this.parentDocumentGroupAdd();
	}

	public void onClick$menuitemParentDocumentGroupRemove(Event event)
			throws Exception {
		this.parentDocumentGroupRemove();
	}
	
	public void onClick$toolbarbuttonParentDocumentGroupAdd(Event event)
			throws Exception {
		this.parentDocumentGroupAdd();
	}

	public void onClick$toolbarbuttonParentDocumentGroupRemove(Event event)
			throws Exception {
		this.parentDocumentGroupRemove();
	}

	public void onClick$menuitemChildDocumentGroupAdd(Event event)
			throws Exception {
		this.childDocumentGroupAdd();
	}

	public void onClick$menuitemChildDocumentGroupRemove(Event event)
			throws Exception {
		this.childDocumentGroupRemove();
	}
	
	public void onClick$toolbarbuttonChildDocumentGroupAdd(Event event)
			throws Exception {
		this.childDocumentGroupAdd();
	}

	public void onClick$toolbarbuttonChildDocumentGroupRemove(Event event)
			throws Exception {
		this.childDocumentGroupRemove();
	}

	private void documentGroupSave() throws Exception {
		if (this.documentGroup.getName() != null) {
			this.documentGroupService.save(this.documentGroup);
			this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
			Clients.evalJavaScript("window.history.back();");
		} else {
			this.message.setText("You must enter 'Group Name'.");
            this.message.setIcon("/img/exclamation.png");
            this.binder.loadAll();
		}
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);

		if (this.execution.getParameter("Id") != null) {
			this.documentGroup = this.documentGroupService.find(Long
					.parseLong(this.execution.getParameter("Id")));
			this.labelTitle.setValue("Edit Document Group");
		} else {
			this.documentGroup = this.documentGroupService
					.create(SecurityContextHolder.getContext()
							.getAuthentication().getName());
			this.labelTitle.setValue("Create Document Group");
		}

		component.setAttribute("controller", this);
	}

	private void documentAdd() throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentSelect.zul", null, null);
		window.setParent(self);
		window.doModal();
		
		if (this.execution.getAttribute("result") == "ok") {
			Document selectedDocument = (Document) this.execution
					.getAttribute("selectedDocument");
			this.documentGroup.getDocuments().add(selectedDocument);
		}

		this.binder.loadComponent(this.listboxDocument);
	}

	private void documentRemove() throws Exception {
		Set<Listitem> selectedItems = this.listboxDocument
				.getSelectedItems();

		for (Listitem selectedItem : selectedItems) {
			Document selectedDocument = (Document) selectedItem
					.getValue();
			this.documentGroup.getDocuments().remove(selectedDocument);
		}

		this.binder.loadComponent(this.listboxDocument);
	}

	private void parentDocumentGroupAdd() throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentGroupSelect.zul", null, null);
		window.setParent(self);
		window.doModal();
		
		if (this.execution.getAttribute("result") == "ok") {
			DocumentGroup selectedDocumentGroup = (DocumentGroup) this.execution
					.getAttribute("selectedDocumentGroup");
			this.documentGroup.addParentDocumentGroup(selectedDocumentGroup);
		}

		this.binder.loadAll();
	}

	private void parentDocumentGroupRemove() throws Exception {
		Set<Listitem> selectedItems = this.listboxParentDocumentGroup
				.getSelectedItems();

		for (Listitem selectedItem : selectedItems) {
			DocumentGroup selectedDocumentGroup = (DocumentGroup) selectedItem
					.getValue();
			this.documentGroup.getParentDocumentGroups().remove(selectedDocumentGroup);
		}

		this.binder.loadComponent(this.listboxParentDocumentGroup);
	}

	private void childDocumentGroupAdd() throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentGroupSelect.zul", null, null);
		window.setParent(self);
		window.doModal();
		
		if (this.execution.getAttribute("result") == "ok") {
			DocumentGroup selectedDocumentGroup = (DocumentGroup) this.execution
					.getAttribute("selectedDocumentGroup");
			this.documentGroup.getChildDocumentGroups().add(selectedDocumentGroup);
		}

		this.binder.loadComponent(this.listboxChildDocumentGroup);
	}

	private void childDocumentGroupRemove() throws Exception {
		Set<Listitem> selectedItems = this.listboxChildDocumentGroup
				.getSelectedItems();

		for (Listitem selectedItem : selectedItems) {
			DocumentGroup selectedDocumentGroup = (DocumentGroup) selectedItem
					.getValue();
			this.documentGroup.getChildDocumentGroups().remove(selectedDocumentGroup);
		}

		this.binder.loadComponent(this.listboxChildDocumentGroup);
	}
}
